import { defineStore } from "pinia";

const Login = name => {
    return new Promise(resolve => {
        setTimeout(() => {
            resolve({
                name: "毛毛",
                age: 18
            });
        }, 1000);
    });
};

export const useStore = defineStore("counter", {
    state: () => {
        return {
            name: "mmm",
            age: 18
        };
    },
    persist: {
        enabled: true, //开启缓存
        Storage: sessionStorage, //缓存方式
        paths: []
    }, //持久化数据
    getters: {
        getNewName() {
            return this.name + "发财致富！" + this.age;
        },
        getAge() {
            return this.age;
        }
    },
    actions: {
        async setName() {
            const res = await Login();
            console.log(res);
            this.name = res.name;
            this.setAge();
        },
        setAge() {
            this.age++;
        }
    }
});
